<?php
namespace Common\Model;

/**
 * User: 黄
 * Date: 2017/4/13
 * Time: 16:36
 */
trait ActivityModel
{
    /**
     * 商品促销价格
     * @param $local_id
     * @param $store_id
     * @return int|mixed
     */
    public function checkGoodsIsPromotion($local_id, $store_id){
        $time = time();
        $lists = M('b2c_store_goods_promotion')
            ->where('local_id = %d and display = 1 and (start_time < %d and end_time > %d)', [$local_id, $time, $time])
            ->field(['store_promotion'])->select();
        $proPrice = [];
        foreach ($lists as $list){
            $proStoreStr = $list['store_promotion'];
            if (empty($proStoreStr)) continue;
            $storeIds = json_decode($proStoreStr, true);
            if (!is_array($storeIds)) continue;
            if (array_key_exists($store_id, $storeIds)){
                $proPrice['price'] = $storeIds[$store_id];
                break;
            }
        }
        return $proPrice;
    }

    public function GoodsPromotion(&$lists, $local_id, $key = 'store_pid'){
        foreach ($lists as $k => $v){
            //商品促销
            $info = $this->checkGoodsIsPromotion($local_id, $v[$key]);
            if (!empty($info)){
                $lists[$k]['proPrice'] = $info['price'];
            }else {
                $lists[$k]['proPrice'] = '';
            }
        }
    }
}